package com.lsy.weizhi2.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.NoteList;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.adapter.MySendAdapter;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.utils.MyOkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 嗨哟 on 2017/3/28.
 */
public class MySendFragment extends Fragment {
    private Context context;
    private RecyclerView recycle_view;
    private MySendAdapter mySendAdapter;
    private List<NoteList.ListBean> list;
    private Gson gson;
    private int pageNo;
    private boolean isload;
    private Handler handler = new Handler();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_send_fragment, container, false);
        recycle_view = (RecyclerView) view.findViewById(R.id.recycle_view);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = getActivity();
        list = new ArrayList<>();
        gson = new Gson();
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recycle_view.setLayoutManager(manager);
        mySendAdapter = new MySendAdapter(context, null);
        recycle_view.setAdapter(mySendAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        pageNo = 1;
        LoadData();
        recycle_view.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                boolean bottom = recyclerView.canScrollVertically(-1);
                if (newState == RecyclerView.SCROLL_STATE_SETTLING && !bottom && !isload) {
                    mySendAdapter.UpData(list, 1, 0);
                    isload = true;
                } else if (newState == RecyclerView.SCROLL_STATE_IDLE && !bottom && isload) {

                    if (pageNo >= 1 && list != null && list.size() >= 10 * pageNo) {
                        mySendAdapter.UpData(list, 1, 1);
                        pageNo++;
                        LoadData();
                    } else {
                        mySendAdapter.UpData(list, 1, 2);
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mySendAdapter.UpData(list, 0, 0);
                            }
                        }, 500);
                    }

                }
            }
        });
    }

    private void LoadData() {
        Map<String, String> map = new HashMap<>();
        map.put("pageNo", pageNo + "");
        map.put("pageSize", 10 + "");
        MyOkHttpUtils.getInstance(context).doGet(Constants.MyOrder, map)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        Log.i("MeSendFragment","message==="+message);
                        if (message != null) {
                            try {
                                JSONObject jsonObject = new JSONObject(message);
                                boolean result = jsonObject.getBoolean("result");
                                if (result) {
                                    if (pageNo == 1) {
                                        list.clear();
                                    }
                                    NoteList noteList = gson.fromJson(message, NoteList.class);
                                    List<NoteList.ListBean> list_bean = noteList.getList();
                                    if (list_bean != null && list_bean.size() != 0) {
                                       /* for (int i = 0; i < list_bean.size(); i++) {
                                            if(list_bean.get(i).getStatus()!=2){
                                                list.add(list_bean.get(i));
                                            }
                                        }*/
                                        list.addAll(noteList.getList());
                                    }
                                    mySendAdapter.UpData(list, 0, 0);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }


}
